package org.dreamwork.assistant.tools.persistence.jfx;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.dreamwork.cli.ArgumentParser;

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainFrame extends Application {
    @FXML private ComboBox<String> cmbFile, cbPackageName, cbOutPath, cbDatabase, cbDatabaseName;
    @FXML private TextField txtServer, txtPort, txtUserName;
    @FXML private PasswordField txtPassword;
    @FXML private Label lblDbServer, lblDbPort, lblDbName, lblUserName, lblPassword;
    @FXML private CheckBox chkJson, cbPrimitive, cbEquals;
    @FXML private TextArea txtLogger;
    
    private Node[] groups;
    private Stage stage;
    private FileChooser chooser = new FileChooser ();
    private GeneratorInfo info = new GeneratorInfo ();
    private Map<String, List<String>> config = new HashMap<> ();

/*
    private ObservableList<String> DB_TYPES = new ObservableListBase<String> () {
        String[] TYPES = {"SQLite", "Derby", "PostgreSQL"};
        @Override
        public String get (int index) {
            return TYPES [index];
        }

        @Override
        public int size () {
            return TYPES.length;
        }
    };
*/

    @Override
    public void start (Stage stage) throws Exception {
        this.stage = stage;
        stage.setTitle ("SQL Generator");
        ClassLoader loader = getClass ().getClassLoader ();
        URL url = loader.getResource ("jfx/persistence.fxml");
        URL icon = loader.getResource ("source_java.png");

        if (icon != null) {
            stage.getIcons ().addAll (new Image (icon.openStream ()));
        }

        if (url != null) {
            GridPane root = FXMLLoader.load (url);
            root.setPadding (new Insets (15, 10, 15, 10));
            Scene scene = new Scene (root);
            stage.setScene (scene);
            stage.show ();

            initComponents (root);
        }
    }

    @SuppressWarnings ("unchecked")
    private void initComponents (Parent root) {
        cbDatabase      = (ComboBox<String>) root.lookup ("#cbDatabase");
        cbDatabaseName  = (ComboBox<String>) root.lookup ("#cbDatabaseName");
        cmbFile         = (ComboBox<String>) root.lookup ("#cmbFile");
        cbPackageName   = (ComboBox<String>) root.lookup ("#cbPackageName");
        cbOutPath       = (ComboBox<String>) root.lookup ("#cbOutPath");
        cbDatabaseName  = (ComboBox<String>) root.lookup ("#cbDatabaseName");

        txtServer       = (TextField) root.lookup ("#txtServer");
        txtPort         = (TextField) root.lookup ("#txtPort");
        txtUserName     = (TextField) root.lookup ("#txtUserName");
        txtPassword     = (PasswordField) root.lookup ("#txtPassword");

        lblDbServer     = (Label) root.lookup ("#lblDbServer");
        lblDbPort       = (Label) root.lookup ("#lblDbPort");
        lblDbName       = (Label) root.lookup ("#lblDbName");
        lblUserName     = (Label) root.lookup ("#lblUserName");
        lblPassword     = (Label) root.lookup ("#lblPassword");

        chkJson         = (CheckBox) root.lookup ("#chkJson");
        cbPrimitive     = (CheckBox) root.lookup ("#cbPrimitive");
        cbEquals        = (CheckBox) root.lookup ("#cbEquals");

        txtLogger       = (TextArea) root.lookup ("#txtLogger");

        groups = new Node[] {
                lblDbName, lblDbServer, lblPassword, lblUserName, lblDbPort,
                cbDatabaseName, txtPassword, txtServer, txtUserName, txtPort
        };

        cbDatabase.getItems ().addAll ("Derby", "SQLite", "PostgreSQL");
    }

    private void setGroupStatus (boolean disabled) {
        for (Node node : groups) {
            node.setDisable (disabled);
        }
    }
    
    @SuppressWarnings ("unchecked")
    private void loadConfig () {
        File file = new File (".", "persistence.conf");
        if (file.exists ()) {
            try (InputStream in = new FileInputStream (file)) {
                ObjectInputStream ois = new ObjectInputStream (in);
                config = (Map<String, List<String>>) ois.readObject ();
            } catch (Exception ex) {
                ex.printStackTrace ();
            }
        }
    }

    @FXML
    public void selectScript () throws IOException {
        ObservableList<FileChooser.ExtensionFilter> filters = chooser.getExtensionFilters ();
        filters.clear ();
        filters.add (new FileChooser.ExtensionFilter ("SQL Script Files", "*.sql"));
        File file = chooser.showOpenDialog (stage);
        if (file != null) {
            info.script = file;
            cmbFile.getItems ().add (file.getCanonicalPath ());
            cmbFile.getEditor ().setText (file.getCanonicalPath ());
        }
    }
}